﻿package com.sz.login.dao.impl;

import java.util.*;

import com.sz.common.ConstDefine;
import com.sz.common.crypt.AESCrypt;
import com.sz.login.dao.ILoginDAO;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class LoginDAOImpl  extends SqlMapClientDaoSupport implements ILoginDAO{
	
	@SuppressWarnings("unchecked")
	public Map verifyLogin(String username, String passwords, String region) throws Exception {
		
		String msg = null;
		Map r = new HashMap();
		
		//根据用户名查询
		Map map = new HashMap();
		map.put("NAME", username);
		map.put("REGION_ID", region);
		List list = getSqlMapClient().queryForList("login_sqlmap.select_manager", map);
		
		if(list.size() <= 0) {
			msg = "用户不存在";
		} else {
			//先看账号是否正常
			if(((Map)list.get(0)).get("STATUS") == null) {
				msg = "账号状态异常";
			} else if(((Map)list.get(0)).get("STATUS").toString().equals("0") == false) {
				msg = "账号被锁定";
			}
			
			if (msg == null) { //在账号正常的前提下比较密码
				String tmp = ((Map) list.get(0)).get("PASSWORDS").toString();
				if (passwords.equals(AESCrypt.decrypttoStr(tmp, ConstDefine.s_cryptKey))) { // 成功
					msg = null; // msg为null, 表示成功
					r.put("MANAGER_ID", ((Map) list.get(0)).get("MANAGER_ID")); // 传出manager_id
					r.put("TYPE", ((Map) list.get(0)).get("TYPE")); //传出账户类型
					r.put("REGION_ID", ((Map) list.get(0)).get("REGION_ID"));
					r.put("REGION_NAME", ((Map) list.get(0)).get("REGION_NAME"));
				} else {
					msg = "密码错误";
				}
			}
		}
		r.put("MESSAGE", msg);
		return r;
	}
	
	@SuppressWarnings("unchecked")
	public void logLogin(Map map) throws Exception {
		getSqlMapClient().insert("login_sqlmap.insert_manager_login", map);
	}

}
